define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'message/index' + location.search,
                    add_url: 'message/add',
                    //edit_url: 'message/edit',
                    del_url: 'message/del',
                    multi_url: 'message/multi',
                    import_url: 'message/import',
                    table: 'message',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                visible: false,
                showToggle: false,
                showColumns: false,
                search: false,
                showExport: false,
                escape: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'sende_data', title: __('Sende_data'), searchList: {"0":__('Sende_data 0'),"1":__('Sende_data 1'),"2":__('Sende_data 2'),"3":__('Sende_data 3'),"4":__('Sende_data 4')}, formatter: Table.api.formatter.normal},
                        {field: 'nicknames', title: __('Nicknames'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,
                            formatter: Table.api.formatter.datetime},
                        {field: 'is_read', title: __('Is_read'), searchList: {"0":__('Is_read 0'),"1":__('Is_read 1')}, formatter: Table.api.formatter.normal},
                        {field: 'read_time', title: __('Read_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false,
                            formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,
                            buttons: [
                                {
                                    name: 'see',
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    text: '查看',
                                    //icon: 'fa fa-folder-o',
                                    url: function (row, column) {
                                        var url = 'message/see?id=' + row.id;
                                        return url;
                                    },
                                },
                            ],
                            events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            var sende_data=$('input[name="row[sende_data]"]').val();
            $("#c-member_ids").data("params", function(){
                return {custom: {status: '1'}};
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $("#c-member_ids").data("params", function(){
                return {custom: {status: '1'}};
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //selecttype 切换redio的id 点击切换改变对应的参数
                var sende_data=$('input[name="row[sende_data]"]').val();
                $('input[name="row[sende_data]"]').click(function () {
                    sende_data=this.value;
                    //发送对象:0=用户,1=店铺,2=区域服务商,3=卡经销商,4=分销商
                    var url="member/index";
                    var field="nickname";
                    if(sende_data==1) url="merchant/index";
                    if(sende_data==2) url="regionservice/index";
                    if(sende_data==3) url="card/dealer/index";
                    if(sende_data==4) url="distributor/index";
                    if(sende_data>0) field="name";
                    $("#c-member_ids").selectPageClear();
                    $("#c-member_ids_text").data("selectPageObject").value = [];
                    $("#c-member_ids_text").data("selectPageObject").option.data = url;
                    $("#c-member_ids_text").data("selectPageObject").option.orderBy = [];
                    $("#c-member_ids_text").data("selectPageObject").option.showField= field;
                    $("#c-member_ids_text").data("selectPageObject").option.searchField= field;
                })
            }
        }
    };
    return Controller;
});


